function [nchan] = exportxls

%  Created 9/8/03 by B. Ross
%  This function will export analysis data out of the Project structure and
%  write it to a Microsoft Excel spreadsheet file.  The function uses the xlswrite.m
%  function that was downloaded from the mathworks website.

%  On 9/9/03 updated Excel filename selection logic to allow user to more easily
%  select the path as well as the filename.

%  Last modified 9/9/03 by B. Ross

%  [number_of_channels_written] = exportxls

% ********************************************************************************

global Project;

nchan = 0;

%  Check for existance of analysis data in current Project
content = fieldnames(Project.Test_Item.Run.Channel(1));
if strcmp(content{size(content,1)},'Analysis')

	%  Prompt for Analysis Number to Export
	
	[anal_num, varargout] = analysis_prompt;
	
	%  Select Channels to Export
	
	[fchan, lchan, varargout] = channel_select;
    nchan = lchan-fchan + 1;

    %  Generate the ordinate & abscissa matrices
    
    for ii = fchan:lchan
        ordinate(:,ii-fchan+1) = Project.Test_Item.Run.Channel(ii).Analysis(anal_num).Ord.';
    end
    abscissa = Project.Test_Item.Run.Channel(fchan).Analysis(anal_num).Abs.';
    
    %  Check for complex data and if complex break into real and imaginary parts
    
	if ~isreal(ordinate)
        for jj = 1:nchan
            data(:,(2*jj)-1) = real(ordinate(:,jj));
            data(:,(2*jj)) = imag(ordinate(:,jj));
        end
        ordinate = data;
    end

	%  Combine abscissa and ordinate data into a single matrix
    m = [abscissa, ordinate];

    %  Generate the vector of column headers
    for kk = fchan:lchan
        respnam{1,kk-fchan+2} = [Project.Test_Item.Run.Channel(kk).Coordinate ...
                               Project.Test_Item.Run.Channel(kk).Direction ...
                               Project.Test_Item.Run.Channel(kk).Orientation];
    end
    if Project.Test_Item.Run.Channel(fchan).Analysis(anal_num).Type == 1
        respnam{1,1} = 'Time';
	else
        respnam{1,1} = 'Frequency';
	end
  
    %  Adjust header for complex data
    for ii = fchan:lchan
        ordinate(:,ii-fchan+1) = Project.Test_Item.Run.Channel(ii).Analysis(anal_num).Ord.';
    end
	if ~isreal(ordinate)
        temp{1} = respnam{1,1};
        for ii = 1:nchan
            temp{2*ii} = respnam{1,ii+1};
            temp{(2*ii)+1} = '';
        end
	    respnam = temp;
	end
	
    %  For now, set the header matrix to the following:
    if isreal(ordinate)
	    head = ['freq, real, real, real, ...'];
    else
        head = ['freq, real, imaginary, real, imaginary, ...'];
    end
    
    %  Prompt for Excel filename and directory using the Project
    %  Setup Filename as the default name.
	
	% 	filename = [Project.Test_Item.Run.Setup_File '.xls']
    [fname, pname] = uiputfile([Project.Test_Item.Run.Setup_File '.xls'],...
                        'Select Export File Name');
	if isequal([fname, pname], [0,0])
	    return
	end
    filename = fullfile(pname, fname);

	%  Generate the file
    xlswrite(m,head,respnam,filename);
    
else % if strcmp(content{size(content,1)},'Analysis')
    analyses_missing_error;
end
